﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace CoolCode.Web.Mvc.UI
{
    public static class StringExtensions
    {
        public static string FormatAsHtmlId(this string name)
        {
            //Replace charactes not valid for ID attribute with underscores.
            //Replace dots with underscores to distinguish from name attribute.
            return string.IsNullOrEmpty(name)
                ? string.Empty
                : Regex.Replace(name, "[^a-zA-Z0-9-:]", "_");
        }

        public static string FormatAsHtmlName(this string name)
        {
            //Replace charactes not valid for name attribute with underscores.
            return string.IsNullOrEmpty(name)
                ? string.Empty
                : Regex.Replace(name, "[^a-zA-Z0-9.-:]", "_");
        }
    }
}
